package cn.dinghu.pro.controller;


import cn.dinghu.pro.common.ResultData;
import cn.dinghu.pro.entity.Admin;
import cn.dinghu.pro.entity.Nurse;
import cn.dinghu.pro.entity.Orders;
import cn.dinghu.pro.entity.User;
import cn.dinghu.pro.service.INurseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Objects;

/**
 * <p>
 * 护工表 前端控制器
 * </p>
 *
 * @author dinghu
 * @since 2025-10-14
 */
@RestController
@RequestMapping("/nurse")
@Api(tags = "护士人员管理")
@ApiSupport(author = "熙神" )
public class NurseController {
    @Autowired
    private INurseService nurseService;

    @GetMapping("/getPage")
    @ApiOperation(value = "获取分页数据",notes = "使用mp实现")
    public ResultData getPage(@RequestParam Integer pageNo, @RequestParam Integer pageSize,
                              String realName,String status){
        /*IPage<Nurse> dictPage= new Page<>(pageNo, pageSize);
        // 构造查询条件
        QueryWrapper<Nurse> queryWrapper = new QueryWrapper<>();
        // 判断 typeCode不为空的时候，进行调节附加
        if(StringUtils.isNotEmpty(realName)){
            queryWrapper.eq("real_name",realName);
        }

        if(StringUtils.isNotEmpty(status)){
            int x=Integer.parseInt(status);
            queryWrapper.eq("status",x);
        }


        IPage<Nurse> page = nurseService.page(dictPage,queryWrapper);
        return ResultData.ok(page);*/
        return nurseService.getPageInfo(realName,status,pageNo,pageSize);
    }
    @DeleteMapping("/deleteById/{id}")
    @ApiOperation(value = "删除ById",notes = "使用mp实现")
    public ResultData deleteById(@PathVariable Long id){
        return nurseService.removeById(id)==true?ResultData.ok():ResultData.fail_201();
    }

    @PutMapping("/update")
    @ApiOperation(value = "更新ById",notes = "使用mp实现")
    public ResultData updateById(Nurse nurse){
        return nurseService.edit(nurse)>0?ResultData.ok_201():ResultData.fail_201();
    }

    @PostMapping("/add")
    @ApiOperation(value = "添加",notes = "使用mp实现")
    public ResultData add(Nurse nurse){
        return nurseService.add(nurse)>0?ResultData.ok_201():ResultData.fail_201();
    }

    @GetMapping("/getById/{id}")
    @ApiOperation(value = "获取ById",notes = "使用mp实现")
    public ResultData getById(@PathVariable Long id){
        return nurseService.getById(id)!=null?ResultData.ok():ResultData.fail_201();
    }

    @PutMapping("/updateStatus")
    @ApiOperation(value = "更新状态ById",notes = "使用mp实现")
    public ResultData updateStatus(@RequestParam int id,@RequestParam int status){
        return nurseService.updateStatus(status,id);
    }


    @PostMapping("/login")
    @ApiOperation(value = "护士登录并返回对象", notes = "登录功能")
    public ResultData<Nurse> login(HttpServletRequest req,
                                   @RequestParam String username,
                                   @RequestParam String password) {

        Nurse nurse = nurseService.login(username, password);
        if (null != nurse) {
            // 将护士信息存入session
            HttpSession session = req.getSession();
            session.setAttribute("nurse", nurse);
            return ResultData.ok(nurse);
        } else {
            return ResultData.error("用户名或密码错误！");
        }
    }

    @GetMapping("getPj")
    @ApiOperation(value = "获取评价信息",notes = "使用mp实现")
    public ResultData getPj(Integer good, Integer bad,  Integer goodbad, Integer nuserId){
        int x = nurseService.getCountPj(good,bad,goodbad,nuserId);
        if (x>0){
            return ResultData.ok(x);
        }else {
            return ResultData.noData();
        }
    }
}
